import { _decorator, Component, Node, Label, Prefab, instantiate, Sprite, color } from 'cc';
const { ccclass, property } = _decorator;
import { UIManager } from '../common/UIManager';
import { Inventory } from './Inventory';
import TreaApi from '../api/TreaApi';
import { TreaIng } from '../entity/TreaIng';
import { EventManager } from '../event/EventManager';
import { UIHeader } from '../common/UIHeader';
import { PrefabEnum } from '../common/PrefabEnum';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
import { Events } from '../entity/Events';

@ccclass('trea')
export class trea extends Component {
    @property(UIHeader)
    uiHeader: UIHeader
    /**探险中+时间 */
    @property(Node)
    underWay: Node
    /**探险等级按钮层 */
    @property(Node)
    btnBox: Node
    /**初级探险按钮 */
    @property(Node)
    novice: Node
    /**中级探险按钮 */
    @property(Node)
    intermediate: Node
    /**高级探险按钮 */
    @property(Node)
    advanced: Node
    /**初级探险精灵 */
    @property(Sprite)
    private noviceSprite: Sprite
    /**中级探险精灵 */
    @property(Sprite)
    private intermediateSprite: Sprite
    /**高级探险精灵 */
    @property(Sprite)
    private advancedSprite: Sprite
    /**当前探险等级 */
    @property(Label)
    wayTitle: Label
    /**宝物库存数量 */
    @property(Label)
    inNumVal: Label
    /**当前探险还需时间 */
    @property(Label)
    wayTime: Label
    /**暂无宝物 */
    @property(Node)
    noneContent: Node
    /**宝库图腾库库存 */
    @property(Node)
    content: Node
    /**库存是array,inventory用来循环 */
    @property(Prefab)
    inventory: Prefab
    /**当前库存自定义数组 */
    itemsList: TreaIng[] = []
    times: number;

    // a:TreaIng[]
    // b:number

    // a2: string[] = []

    // b1:number[] = []
    start() {
        this.uiHeader.setData('宝库')
        EventManager.instance.on('宝库close', () => {
            UIManager.instance.closeUI(PrefabEnum.Trea)
        }, this)

        this.noneContent.active = false
        this.underWay.active = false
        this.novice.on(Node.EventType.TOUCH_END, this.noviceBtn, this)
        this.intermediate.on(Node.EventType.TOUCH_END, this.intermediateBtn, this)
        this.advanced.on(Node.EventType.TOUCH_END, this.advancedBtn, this)

        this.getList(1)

        EventManager.instance.on(EventManager.EventType.RefreshTreaList, this.getList, this)
    }

    async noviceBtn() {
        
        if (this.itemsList.length >= 5) {
            UIManager.instance.tip('库存已满')
            return
        } else {
            TreaApi.totemFounding(27, "founding", 0, 1)
            this.getList(2)
        }
        // UIManager.instance.openUI(PrefabEnum.Recording)
    }
    intermediateBtn() {
        if (this.itemsList.length >= 5) {
            UIManager.instance.tip('库存已满')
            return
        } else {
            TreaApi.totemFounding(27, "founding", 0, 2)
            this.getList(2)
        }

    }
    advancedBtn() {
        if (this.itemsList.length >= 5) {
            UIManager.instance.tip('库存已满')
            return
        } else {
            TreaApi.totemFounding(27, "founding", 0, 3)
            this.getList(2)
        }

    }

    timeFun(time: number) {
        var time = time
        //倒计时结束 
        if (time == 0) {
            console.log(time, "计时器结束")
            clearInterval(this.times)
            // 隐藏探险中  显示按钮
            this.btnBox.active = true
            this.underWay.active = false
            this.noneContent.active = false
            this.getList(2)
            return
        }
        var days = Math.floor(time / 1440 / 60);
        var hours = Math.floor((time - days * 1440 * 60) / 3600);
        var minutes = Math.floor((time - days * 1440 * 60 - hours * 3600) / 60);
        var seconds = (time - days * 1440 * 60 - hours * 3600 - minutes * 60);
        return hours + ":" + minutes + ":" + seconds
    }
    /**请求宝库数据方法  parms1:进入页面请求   parms2:点击探险请求之后再次请求 */
    async getList(i: number) {
        
        const apiList = await TreaApi.totemQuery()
        console.log(apiList)
        // 判断如果（正在探险中）es大于0,有正在进行中的探险,隐藏按钮现实探险中时间
        if (apiList.data.es.length > 0) {
            /**前往TreaIng重新自定义正在探险中数据 */
            const esList = Events.getEsList(apiList.data.es)
            /**获取当前时间戳 不含毫秒 */
            const currentTime = Math.floor(UserInfo.instance.serverTime / 1000)
            /**结束时间-当前时间=剩余时间(countdown) */
            let countdown = Math.abs(Number(esList[0].eStartTime) - currentTime)
            this.wayTime.string = Utils.secondToStr(countdown)
            this.times = setInterval(() => {
                this.wayTime.string = Utils.secondToStr(countdown)
                countdown--
                if (countdown == 0) {
                    console.log(countdown, "计时器结束")
                    clearInterval(this.times)
                    // 隐藏探险中  显示按钮
                    this.btnBox.active = true
                    this.underWay.active = false
                    this.noneContent.active = false
                    this.getList(2)
                }
            }, 1000);

            this.wayTitle.string = esList[0].treaName
            
            this.btnBox.active = false
            this.underWay.active = true
            this.underWay.position.set(0)
            if (i == 2) {
                return
            }
        }

        /**宝物库存数组 */
        this.itemsList = TreaIng.createByTreaIngObj(apiList.data.items)
        this.inNumVal.string = '宝物库存 (' + this.itemsList.length + '/5)'
        // 判断如果库存数量等于5,宝库已满,禁止探险
        if (this.itemsList.length == 5) {
            this.noviceSprite.color = color(255,255,255,100)
            this.intermediateSprite.color = color(255,255,255,100)
            this.advancedSprite.color = color(255,255,255,100)
        } else {
            this.noviceSprite.color = color(255,255,255,255)
            this.intermediateSprite.color = color(255,255,255,255)
            this.advancedSprite.color = color(255,255,255,255)
        }
        if (this.itemsList.length == 0) {
            this.noneContent.active = true
        }
        /**宝库库存根据循环新建标签 */
        this.content.destroyAllChildren()
        for (let item of this.itemsList) {
            let gameObject = instantiate(this.inventory)
            this.content.addChild(gameObject)
            gameObject.getComponent(Inventory).setData(item)
        }

    }
}


